﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

public class Shot
{
    public SpriteEffects flip = SpriteEffects.None;

    public Animation shotAnimation;
    public AnimationPlayer sprite;

    private int xposition;
    public int XPosition
    {
        get { return xposition; }
    }

    private int yposition;
    public int YPosition
    {
        get { return yposition; }
    }

    private Direction direction;
    public Direction Direction
    {
        get { return direction; }
    }

    private bool alive;
    public bool Alive
    {
        get { return alive; }
    }

    private int speed;
    public int Speed
    {
        get { return speed; }
    }

    private Texture2D shotImage;
    public Texture2D ShotImage
    {
        get { return shotImage; }
    }

    public Tower mother;

    protected Texture2D testCollider;

    public Shot(int xposition, int yposition, Direction dir, Tower mother)
    {
        this.xposition = xposition;
        this.yposition = yposition;
        this.direction = dir;
        speed = 3;
        alive = true;
        this.mother = mother;

        if (direction == Direction.Right || direction == Direction.Left)
            shotImage = GameSession.Content.Load<Texture2D>("Textures/VisualEffects/tiro_torre");
        else
            shotImage = GameSession.Content.Load<Texture2D>("Textures/VisualEffects/tiro_torrelado");

        testCollider = GameSession.Content.Load<Texture2D>("Textures/Characters/testCollider");

        shotAnimation = new Animation(shotImage, 0.3f, false);
    }

    public bool Update()
    {
        if (direction == Direction.Up)
        {
            yposition -= speed;
        }
        if (direction == Direction.Down)
        {
            yposition += speed;
        }
        if (direction == Direction.Left)
        {
            xposition -= speed;
        }
        if (direction == Direction.Right)
        {
            xposition += speed;
        }

        int xpos = xposition / 64;
        int ypos = yposition / 64;

        if(xpos < 0 || xpos > 19 || ypos < 0 || ypos > 11 )
            alive = false;
        else
        if (GameSession.CollisionMatriz[xpos, ypos] == 1
               || (GameSession.CollisionMatriz[xpos, ypos] == 2 && GameSession.CanCollide(xpos, ypos) && GameSession.GetObject(xpos,ypos).Kind != ObjectKind.Tower ))
        {
            alive = false;
        }

        foreach(Characters chara in GameSession.CharactersList)
        {
            if ((new Rectangle(xposition - 17, yposition -50, 37, 37)).Intersects(chara.BoundingRectangle))
            {
                alive = false;
                if (chara.State == Characters.States.Alive && chara.GetType() == typeof(KeiJow) || chara.GetType() == typeof(Manteiga))
                {
                    chara.TakeDamage(1);
                }
            }
        }

        return alive;
    }

    public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
    {
        // Flip the sprite to face the way we are moving.

        // Draw that sprite.

        if(direction == Direction.Down)
            sprite.Draw(gameTime, spriteBatch, new Vector2(xposition, yposition + GameSession.screenAdjustment), SpriteEffects.FlipVertically);
        else
            if (direction == Direction.Right)
                sprite.Draw(gameTime, spriteBatch, new Vector2(xposition, yposition + GameSession.screenAdjustment), SpriteEffects.FlipHorizontally);
            else
                sprite.Draw(gameTime, spriteBatch, new Vector2(xposition, yposition + GameSession.screenAdjustment), SpriteEffects.None);

        //Test Bounding Size
        //spriteBatch.Draw(testCollider, new Vector2(xposition - 17 , yposition - 50 + GameSession.screenAdjustment),
        //  new Rectangle(0, 0, 37, 37), Color.White);
    }
}
